<template>
  <div>
    <category :scene="scene" />
  </div>
  <div style="margin-top: 20px">
    <el-card class="box-card">
      <div v-show="scene == 0">
        <el-button type="primary" icon="Plus" @click="addArrt">
          <!--   :disabled="categoryStore.c3Id ? false : true" -->
          添加平台属性
        </el-button>
        <el-table border style="margin-top: 10px" :data="attrArr">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="100px"
          ></el-table-column>
          <el-table-column
            label="属性名称"
            min-width="100px"
            prop="attrName"
          ></el-table-column>
          <el-table-column label="属性值名称" min-width="200px">
            <template #default="{ row }">
              <el-tag
                v-for="(item, index) in row.attrValueList"
                :key="index"
                style="margin-right: 5px"
              >
                {{ item.valueName }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200px">
            <template #default="{ row }">
              <el-button
                type="primary"
                icon="Edit"
                @click="editData(row)"
              ></el-button>
              <el-popconfirm
                :title="`是否确认删除${row.attrName}?`"
                @confirm="delData(row.id)"
              >
                <template #reference>
                  <el-button type="primary" icon="Delete"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="scene == 1">
        <el-form :inline="true">
          <el-form-item label="属性名称">
            <el-input
              placeholder="请输入属性名称"
              v-model="attrParmes.attrName"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          icon="Plus"
          :disabled="attrParmes.attrName == '' ? true : false"
          @click="addAttrValue"
        >
          添加属性值
        </el-button>
        <el-button @click="cancel">取消</el-button>
        <el-table
          border
          style="margin: 10px 0px"
          :data="attrParmes.attrValueList"
        >
          <el-table-column
            width="80px"
            type="index"
            label="序号"
            align="center"
          ></el-table-column>
          <el-table-column label="属性值名称">
            <template #default="{ row, $index }">
              <el-input
                v-model="row.valueName"
                placeholder="请输入属性值名称"
                @blur="toLook(row, $index)"
                :ref="(vc:any)=>arrtArrry[$index]=vc"
                v-if="row.flag"
              ></el-input>
              <div v-else @click="toEdit(row, $index)">{{ row.valueName }}</div>
            </template>
          </el-table-column>
          <el-table-column label="属性值操作">
            <template #default="{ row, $index }">
              <el-button
                icon="Delete"
                type="danger"
                @click="attrParmes.attrValueList.splice($index, 1)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button
          type="primary"
          :disabled="attrParmes.attrValueList.length > 0 ? false : true"
          @click="save"
        >
          保存
        </el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-card>
  </div>
</template>
<script lang="ts" setup>
import { reqAttr, AddOrUpdataAttr, delAttr } from '@/api/product/attr'
import {
  c1CodeAttr,
  attributeData,
  attributeDatas,
} from '@/api/product/attr/type'
import useCategoryStore from '@/store/modules/category'
import { ElMessage } from 'element-plus'
import { nextTick, onBeforeMount, reactive, ref, watch } from 'vue'
let categoryStore = useCategoryStore()
let attrArr = ref<attributeData[]>([])
// 新增|修改Data
let attrParmes = reactive<attributeData>({
  attrName: '',
  categoryId: '',
  categoryLevel: 3,
  attrValueList: [],
})
// 组件实例
let arrtArrry = ref<any>([])
// 卡片切换控制
let scene = ref<number>(0) //0显示tabel  1展示修改
watch(
  () => categoryStore.c3Id,
  () => {
    attrArr.value = []
    if (!categoryStore.c3Id) return
    getAttr()
  },
)
// 获取数据
const getAttr = async () => {
  const { c1Id, c2Id, c3Id } = categoryStore
  const res: c1CodeAttr = await reqAttr(c1Id, c2Id, c3Id)
  if (res.code == 200) {
    attrArr.value = res.data
  }
}
// 添加属性回调
const addArrt = () => {
  // 清空数据
  Object.assign(attrParmes, {
    attrName: '',
    categoryId: categoryStore.c3Id,
    categoryLevel: 3,
    attrValueList: [],
  })
  scene.value = 1
}
// 修改回调
const editData = (val: attributeDatas) => {
  Object.assign(attrParmes, JSON.parse(JSON.stringify(val)))
  scene.value = 1
}
// 删除回调
const delData = async (val: number) => {
  let res: any = await delAttr(val)
  if (res.code == 200) {
    ElMessage.success('删除成功')
    getAttr()
  } else {
    ElMessage.error('删除失败')
  }
}
//取消回调
const cancel = () => {
  scene.value = 0
}
// 添加属性值按钮
const addAttrValue = () => {
  attrParmes.attrValueList.push({
    valueName: '',
    flag: true,
  })
  nextTick(() => {
    arrtArrry.value[attrParmes.attrValueList.length - 1].focus()
  })
}
//保存
const save = async () => {
  const res: any = await AddOrUpdataAttr(attrParmes)
  if (res.code == 200) {
    scene.value = 0
    ElMessage.success(attrParmes.id ? '修改成功' : '添加成功')
    getAttr()
  } else {
    ElMessage.error(attrParmes.id ? '修改失败' : '添加失败')
  }
}
// input失去焦点
const toLook = (val: attributeDatas, index: number) => {
  if (val.valueName.trim() == '') {
    ElMessage.warning('属性值不能为空')
    attrParmes.attrValueList.splice(index, 1)
    return
  }
  let attrIf = attrParmes.attrValueList.find((item) => {
    // 当前属性值扣除判断
    if (item != val) {
      return item.valueName == val.valueName
    }
  })
  if (attrIf) {
    ElMessage.error('属性值不能重复')
    attrParmes.attrValueList.splice(index, 1)
    return
  }
  val.flag = false
}
//div点击事件
const toEdit = (val: attributeDatas, index: number) => {
  val.flag = true
  nextTick(() => {
    arrtArrry.value[index].focus()
  })
}
// 路由跳转重置
onBeforeMount(() => {
  // 清空分类仓库数据
  categoryStore.$reset()
})
</script>
<style lang="scss" scoped></style>
